<?php
/**
 * Created by PhpStorm.
 * User: GXF
 * Date: 15-10-14
 * Time: 上午10:31
 */
date_default_timezone_set('PRC');
include 'createTables.php';
session_start();
$action = $_GET['action'];
$db = new SQLite3('MedicalWifi.db');//连接数据库
createDB();
/********************************************************
 ****************************患者新增的病历***************
 * ******************************************************/
if($action == 'newResume'){
    $patientName = $_POST['name'];
    $sex = $_POST['sex'];
    $birthday = $_POST['birthday'];
    $telephone = $_POST['telephone'];
    $medicareCard = $_POST['medicareCard'];
    $registrationNumber = $_POST['registrationNumber'];
    $conditionDescription = $_POST['conditionDescription'];
    $createTime = date("Y-m-d H:i:s");
    $flag = 1;
    while($flag){
        $a = mt_rand(10000000,99999999);
        $b = mt_rand(10000000,99999999);
        $id=$a.$b;//随机生成20位不重复id
        $sql = "select * from patientResume where id = '$id'";
        $result = $db->query($sql);
        if(!($row = $result -> fetchArray(SQLITE3_ASSOC)) ){//若数据库中不存在此id
            $flag = 0;
        }
    }
    //插入此条病历
    $sql = "INSERT INTO patientResume VALUES ('{$id}','{$patientName}','{$sex}','{$birthday}','{$telephone}',
            '{$medicareCard}','{$registrationNumber}','{$conditionDescription}',1,0,'{$createTime}')";
    $result=$db->exec($sql);
    if($result)
        echo json_encode(array('status' => 'success'));
    else
        echo json_encode(array('status' => 'failed'));
}
/*****************************************************************************************
 ****************************查看详情页面**************************************************
 * ***************************************************************************************/
else if($action == 'detail'){
    $doctorID = $_SESSION['username'];
    $resumeID = $_GET['id'];
    $sql ="select * from patientResume where id ='$resumeID'";
    $result = $db->query($sql);
    while($row = $result->fetchArray()) {
        $data = $row;
    }
    if($data){
        echo json_encode(array('status'=>'success','data'=>$data));
    }
    else{
        echo json_encode(array('status'=>'failed','data'=>''));
    }
}
/*****************************************************************************************
 ****************************在待确定页面，点击详情按钮*********************************
 * **************************查看转诊详情***********************************************
 * ***************************************************************************************/
else if($action == 'transferDetail'){
    $doctorID = $_SESSION['username'];
    $resumeID = $_GET['id'];
    $sql ="select * from patientResume where id ='$resumeID'";
    $result = $db->query($sql);
    while($row = $result->fetchArray()) {
        $data = $row;
        $sql1 ="select * from transferRecord where resumeID ='$resumeID'";
        $result1 = $db->query($sql1);
        while($row1 = $result1->fetchArray()){
            $data['doctorID'] = $row1['doctorID'];
            $data['transferDoctorID'] = $row1['transferDoctorID'];
            $data['transferSure'] = $row1['transferSure'];
            $data['transferDiagnosis'] = $row1['transferDiagnosis'];
            if($doctorID == $row1['doctorID'])//我转出的
                $transferDoctorID = $row1['transferDoctorID'];
            else
                $transferDoctorID = $row1['doctorID'];
            $sql2 = "select * from doctor where id='$transferDoctorID'";
            $result2 = $db->query($sql2);
            while($row2 = $result2->fetchArray()){
                $data['hospital'] = $row2['hospital'];
                $data['transferDoctorName'] = $row2['name'];
            }
        }
    }
    if($data){
        echo json_encode(array('status'=>'success','data'=>$data));
    }
    else{
        echo json_encode(array('status'=>'failed','data'=>''));
    }
}